package com.bistu.xiong.tulingrobotchat.frags.main;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.bistu.xiong.common.app.PresenterFragment;
import com.bistu.xiong.common.widget.recycler.RecyclerAdapter;
import com.bistu.xiong.factory.bean.DataWithTag;
import com.bistu.xiong.factory.model.api.community.GetUserPostRspModel;
import com.bistu.xiong.factory.persistence.Account;
import com.bistu.xiong.factory.presenter.community.CommunityContract;
import com.bistu.xiong.factory.presenter.community.CommunityPresenter;
import com.bistu.xiong.factory.test_data.CommunityData;
import com.bistu.xiong.tulingrobotchat.R;
import com.bistu.xiong.tulingrobotchat.adapter.CommunityRecyAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CommunityFragment extends PresenterFragment<CommunityContract.Presenter>
        implements CommunityContract.View,RecyclerAdapter.AdapterListener,SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.appbar)
    View appBar;

    @BindView(R.id.community_recyclerView)
    RecyclerView communityRecyclerView;

    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private List<DataWithTag> dataList = new ArrayList<>();
    private CommunityRecyAdapter communityRecyAdapter;

    public CommunityFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_main_community;
    }

    @Override
    protected void initArgs(Bundle bundle) {
        super.initArgs(bundle);
        dataList = CommunityData.getCommunityData();
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        swipeLayout.setColorSchemeColors(Color.BLUE,
                Color.GREEN,
                Color.YELLOW,
                Color.RED);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeLayout.setDistanceToTriggerSync(300);
        // 设定下拉圆圈的背景
        swipeLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        // 设置圆圈的大小
//        swipeLayout.setSize(SwipeRefreshLayout.LARGE);

        //设置下拉刷新的监听
        swipeLayout.setOnRefreshListener(this);

        //使用Glide加载图片给Appbar设置背景
        Glide.with(this)
                .load(R.drawable.bg_src_aurora)
                .centerCrop()
                .into(new ViewTarget<View,GlideDrawable>(appBar) {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        this.view.setBackground(resource.getCurrent());
                    }
                });
        communityRecyAdapter = new CommunityRecyAdapter(dataList,this,getContext());
        communityRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        communityRecyclerView.setAdapter(communityRecyAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.freshPostData();
    }

    /**
     * 刷新成功之后
     */
    @Override
    public void freshSuccess(ArrayList<GetUserPostRspModel> getUserPostRspModels) {
        toast("刷新成功！");
        dataList = new ArrayList<>();
        dataList.add(new DataWithTag(Account.getCurrentUser(),DataWithTag.USER_INFO));
        for (int i=0;i<getUserPostRspModels.size();i++){
            if (i==0){
                dataList.add(new DataWithTag(getUserPostRspModels.get(i).toUserPost(),DataWithTag.TOPIC_FOR_TODAY));
            }else{
                dataList.add(new DataWithTag(getUserPostRspModels.get(i).toUserPost(),DataWithTag.USER_POST));
            }
        }
        communityRecyAdapter.replace(dataList);

    }

    @Override
    protected CommunityContract.Presenter initPresenter() {
        return new CommunityPresenter(this);
    }

    @Override
    public void onItemClick(RecyclerAdapter.ViewHolder holder, Object o) {

    }

    @Override
    public void onItemLongClick(RecyclerAdapter.ViewHolder holder, Object o) {

    }

    @Override
    public void onRefresh() {
        mPresenter.freshPostData();
    }

    public void stopSwipeRefresh(){
        swipeLayout.setRefreshing(false);
    }

}
